<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Forpastime <Forpastime@qq.com>
// +----------------------------------------------------------------------
namespace api\partner\controller;

use api\partner\model\NoticeModel;
use api\partner\model\PartnerModel;
use api\partner\model\ShopApplyOrderModel;
use cmf\controller\RestBaseController;
use think\Db;
use think\db\Query;
use think\facade\Cache;
use think\facade\Request;
use api\partner\validate\OrderValidate;
use think\Validate;

class UserController extends RestBaseController
{
    protected $token = '';

    public function initialize()
    {
        $token = $this->request->header('XX-Token');

        $id = $this->request->param('partner_id',0,'intval');
        if(!$id){
            $this->error('请求失败');
        }
        // $token_cache = Cache::get('partner:'.$id);
        // if(!$token || $token!=$token_cache){
        //     $this->error('请重新登录');
        // }else{
        //     $this->token = $token_cache;
        // }
    }

    /**
     * 修改密码
     * @param password 密码
     * @param old_password 密码
     */
    public function resetPwd()
    {
        $user_id = $this->request->param('partner_id','0','intval');
        
        $password = $this->request->param('password','');
        $old_password = $this->request->param('old_password','');

        $validate = Validate::make([
                'password'  => 'require|max:25|min:6',
                'old_password' => 'require'
            ],[
                'password.require'=>'新密码不能为空',
                'password.max'=>'密码长度为6-25位字符',
                'password.min'=>'密码长度为6-25位字符',
                'old_password.require'=>'旧密码不能为空'
            ]
        );

        $data = [
            'password'  => $password,
            'old_password' => $old_password
        ];

        if (!$validate->check($data)) {
            $this->error('请求失败',$validate->getError());
        }

        $user = Db::name('partner')->where('id',$user_id)->where('password',cmf_password($old_password))->find();
        if(!$user){
            $this->error('旧密码不正确');
        }else{
            $up = Db::name('partner')->where('id',$user_id)->update(['password'=>cmf_password($password)]);
            if($up){
                $this->success('修改密码成功');
            }else{
                $this->error('修改密码失败');
            }
        }
    }

    /**
     * 我的账户首页
     */
    public function account()
    {
        $user_id = $this->request->param('partner_id','0','intval');
        $info = Db::name('partner')->where('id',$user_id)
            ->field('name,create_time,tel,idcard,company,money,unicode,status')
            ->withAttr('name',function($v){
                return '*'.mb_substr($v,-1);
            })
            ->withAttr('money',function($v){
                return number_format($v/100,2);
            })
            ->find();
        $lastStore = Db::name('partner_gongjijin_record')->where('type',1)->where('partner_id',$user_id)->order('create_time desc')
            ->withAttr('money',function($v){
                return number_format($v/100,2);
            })
            ->withAttr('create_time',function($v){
                return date('Y-m-d',strtotime($v));
            })
            ->find();
        $lastGet = Db::name('partner_gongjijin_record')->where('type',2)->where('partner_id',$user_id)->order('create_time desc')
            ->withAttr('money',function($v){
                return number_format($v/100,2);
            })
            ->withAttr('create_time',function($v){
                return date('Y-m-d',strtotime($v));
            })
            ->find();

        $data['info']=$info;
        $data['store']=$lastStore;
        $data['get']=$lastGet;

        $this->success('请求成功',$data);
    }

    /**
     * 更多信息
     */
    public function accountMore()
    {
        $user_id = $this->request->param('partner_id','0','intval');
        $info = Db::name('partner_gongjijin')->where('partner_id',$user_id)->find();
        if($info){
            $last_month = Db::name('partner_gongjijin_record')->where('partner_id',$user_id)->where('type',1)->value('create_time');
            $info['base_line'] = number_format($info['base_line'],2);
            $info['personal_money'] = number_format($info['personal_money'],2);
            $info['company_money'] = number_format($info['company_money'],2);

            $info['personal_rate'] = number_format($info['personal_rate'],2).'%';
            $info['company_rate'] = number_format($info['company_rate'],2).'%';
            $info['last_month'] = date('Y-m',strtotime($last_month));

            $this->success('请求成功',$info);
        }else{
            $this->error('暂无数据');

        }
    }

    /**
     * 我的列表
     */
    public function lists()
    {
        $user_id = $this->request->param('partner_id','0','intval');
        $page = $this->request->param('page','1','intval');
        $status = $this->request->param('status','0','intval');

        $lists = Db::name('partner_gongjijin_record')
            ->field('*,create_time as year')
            ->where(['partner_id'=>$user_id])
            ->where(function (Query $query) use ($status){
                if($status > 0){
                    $query->where('type',$status);
                }
            })
            ->withAttr('year',function($v){
                return date('Y',strtotime($v));
            })
            ->withAttr('create_time',function($v){
                return date('m-d',strtotime($v));
            })
            ->withAttr('money',function($v){
                return number_format($v/100,2);
            })
            ->order('create_time desc')
            ->limit(10,($page-1)*10)
            ->select()
            ->toArray();

        $this->success('请求成功',$lists);
    }


}